use axum::{
    http::{Method, StatusCode, Uri},
    response::IntoResponse,
    Json,
};
use serde_json::json;
use tower::{timeout::error::Elapsed, BoxError};

use tracing::error;

use crate::system::error::ErrorCode;


pub(crate) async fn police_error(method: Method, uri: Uri, error: BoxError) -> impl IntoResponse {
    error!("{} {} police_error:{}.", method, uri, error);
    if error.is::<Elapsed>() {
        (
            StatusCode::REQUEST_TIMEOUT,
            Json(json!(ErrorCode::TIMEOUT.to_web_ret())),
        )
    } else {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!(ErrorCode::INTERNAL_ERROR.to_web_ret())),
        )
    }
}

pub(crate) async fn not_found_error() -> impl IntoResponse {
    (
        StatusCode::NOT_FOUND,
        Json(json!(ErrorCode::NOT_FOUND.to_web_ret())),
    )
}
